package mest.service.hank.impl;

import mest.dao.hank.plan_productjob_takematerial_recordMapper;
import mest.entity.plan_productjob_takematerial_record;
import mest.service.hank.PlanProductjobTakematerialRecordService;
import mest.service.hank.PlanProductjobTakematerialService;
import mest.util.NumberConst;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class PlanProductjobTakematerialRecordServiceImpl implements mest.service.hank.PlanProductjobTakematerialRecordService {

    @Resource
    private plan_productjob_takematerial_recordMapper pptrm;
    @Resource
    private PlanProductjobTakematerialService pjts;

    @Override
    public int deleteByPrimaryKey(Integer id,String theirDb) {
        return pptrm.deleteByPrimaryKey(id, theirDb);
    }

    @Override
    public int insert(plan_productjob_takematerial_record record,String theirDb) {
        return pptrm.insert(record, theirDb);
    }

    @Override
    public int insertSelective(plan_productjob_takematerial_record record,String theirDb) {
        return pptrm.insertSelective(record, theirDb);
    }

    @Override
    public plan_productjob_takematerial_record selectByPrimaryKey(Integer id,String theirDb) {
        return pptrm.selectByPrimaryKey(id, theirDb);
    }

    @Override
    public int updateByPrimaryKeySelective(plan_productjob_takematerial_record record,String theirDb) {
        return pptrm.updateByPrimaryKeySelective(record, theirDb);
    }

    @Override
    public int updateByPrimaryKey(plan_productjob_takematerial_record record,String theirDb) {
        return pptrm.updateByPrimaryKey(record, theirDb);
    }

    /**
     * 插入plan的操作记录,同时 生成仓库领料单
     */
    @Override
    public int intsertList(Map<String, Object> map) throws Exception{
    	//插入操作记录
    	Object list = map.get("list");
        pptrm.intsertList((List<plan_productjob_takematerial_record>)list , map.get("theirDb")+"");

        pjts.updateByone(map.get("theirDb")+"", (List<plan_productjob_takematerial_record>)list);



        //获取仓库领料单编码
        map.put("tableNumber", NumberConst.WareHouseTakematerial_No + "-" + pptrm.getWareHouseTakematerialNumber(map));
        int i = pptrm.updateWareHouseTakematerialNumber(map);
        if (i != 1) {
        	throw new Exception("仓库领料流水编码新增失败");
		}
    	//生成仓库单表头
        map.remove("list");
		map.put("wtId", -1);
		i = pptrm.intsertWarehouseTakematerial(map);
        if (i != 1) {
        	throw new Exception("仓库领料单表头新增失败");
		}
        
        map.put("list", list);
		System.out.println(map.toString());
		//获取wtId后生成表体
		if ((int) map.get("wtId") > 0) {
			i = pptrm.insertWareHousePJTakematerial(map);
		} else {
			throw new Exception("仓库领料单主键没生成");
			// return (int) map.get("id");
		}
		
        return i;
    }


}